; (function (window, $) {
  var infobj = null

  $(function () {
    // just a datepicker
    $('input.datepicker').datepicker({
      language: 'zh-CN',
      format: 'yyyy-mm-dd'
    })
    getinfo()

    //when a file upload,get the oss url to hidden input

    $('input[type="file"]').on('change', function () {
      var fileInput = $(this)

      util.fileUpload(fileInput[0].files[0], function (res) {
        fileInput.prev('span').text('重新上传')
        fileInput.next('input[type="hidden"]').val(res.data)
      })
    })

    $('.uploadbox').hover(
      function () {
        var imgUrl = $(this).find("input[type='hidden']").val()
        if (imgUrl !== '') {
          $(this).find('img').attr('src', imgUrl)
          $(this).find('a').attr('href', imgUrl).show()
        }
      },
      function () {
        $(this).find('a').show()
      }
    )

    $('#savebtn').on('click', saveinfo)

    $('textarea[name="profiles"]').on('keyup', function () {
      var len = $(this).val().length;
      if (len > 200) {
        layer.open({
          content: "内容长度不能超过200个字符",
          btn: "确定"
        })
        $(this).val($(this).val().substr(0, 199));
      }
    })
  })

  function saveinfo() {
    if (!submitCheck()) {
      return
    }

    // get all text
    $('input[type="text"]').each(function () {
      var key = $(this).attr('name')
      var val = $(this).val()
      infobj[key] = val
    })

    // get all select
    $('select').each(function () {
      var key = $(this).attr('name')
      var val = $(this).val()
      infobj[key] = val
    })

    // get all radio
    $('input[type="radio"]:checked').each(function () {
      var key = $(this).attr('name')
      var val = $(this).val()
      infobj[key] = val
    })

    // all hidden,if a hidden is empty,do not let it edit infobj
    $('input[type="hidden"]').each(function () {
      var val = $(this).val()
      if (!val) {
        return
      }
      var key = $(this).attr('name')
      infobj[key] = val
    })

    // all textarea
    $('textarea').each(function () {
      var val = $(this).val()
      if (!val) {
        return
      }
      var key = $(this).attr('name')
      infobj[key] = val
    })

    //delete some pro

    delete infobj.id
    delete infobj.status
    delete infobj.userType
    delete infobj.phone
    delete infobj.score
    delete infobj.successCount
    delete infobj.auditStatus

    console.log(infobj)

    util.ajax({
      url: '/manager/profile/save',
      type: 'POST',
      data: infobj,
      success: function (res) {
        util.flushUserInfo(function () {
          layer.alert('保存成功', {
            skin: 'demo-class',
            closeBtn: 0
          }, function () {
            window.location.href = '/channel/myprofile.html';
          });
        })
      }
    })
  }

  function getinfo() {
    util.ajax({
      type: 'GET',
      url: '/user/manager',
      success: function (res) {
        infobj = res.data
        console.log(infobj)

        // s1
        $('input[name="name"]').val(infobj.name)
        $('input[name="telPhone"]').val(infobj.telPhone)
        $('input.datepicker').datepicker('setDate', new Date(infobj.birthday))
        $('input[name="avatar"]').val(infobj.avatar)

        $('input[name="marryType"][value="' + infobj.marryType + '"]').prop(
          'checked',
          'checked'
        )
        $('input[name="sex"][value="' + infobj.sex + '"]').prop(
          'checked',
          'checked'
        )

        $('select[name="provinceId"]').val(infobj.provinceId)
        $('select[name="cityId"]').val(infobj.cityId)

        $('span#user-name').text(infobj.name)
        $('span#user-company').text(infobj.companyName)
        $('span#profile-status').text(
          parseInt(infobj.isProfile) === 1 ? '已认证' : '未认证'
        )
        $('img#headshow').attr('src', infobj.avatar)

        // s2
        $('input[name="idCard"]').val(infobj.idCard)
        $('input[name="idCardFront"]').val(infobj.idCardFront)
        $('input[name="idCardVerso"]').val(infobj.idCardVerso)
        $('input[name="idCardPhoto"]').val(infobj.idCardPhoto)

        // s3
        $("input[name='companyName']").val(infobj.companyName)
        $("textarea[name='profiles']").val(infobj.profiles)
        $("select[name='headship']").val(infobj.headship)
        $("input[name='businessCard']").val(infobj.businessCard)
        $("input[name='contract']").val(infobj.contract)
        $("input[name='workCard']").val(infobj.workCard)
      }
    })
  }

  function formatDate(timestamp) {
    var date = new Date(timestamp)
    return (
      date.getFullYear +
      '-' +
      parseInt(date.getMonth() + 1) +
      '-' +
      date.getDate()
    )
  }

  // screen 0 to 1 check
  function submitCheck() {
    if ($("input[name='name']").val() === '') {
      $("input[name='name']").css('border-color', '#FF0000')
      return false
    }

    if ($("input[name='birthday']").val() === '') {
      $("input[name='birthday']").css('border-color', '#FF0000')
      return false
    }

    if ($("input[name='telPhone']").val() === '') {
      $("input[name='telPhone']").css('border-color', '#FF0000')
      return false
    }

    if ($("input[name='idCard']").val() === '') {
      $("input[name='idCard']").css('border-color', '#FF0000')
      return false
    }

    if ($("input[name='companyName']").val() === '') {
      $("input[name='companyName']").css('border-color', '#FF0000')
      return false
    }

    if ($("input[name='companyName']").val() === '') {
      $("input[name='companyName']").css('border-color', '#FF0000')
      return false
    }

    if (
      $("input[name='businessCard']").val() === '' &&
      $("input[name='workCard']").val() === '' &&
      $("input[name='contract']").val() === ''
    ) {
      layer.alert('名片 工牌 合同 至少选择一种上传', {
        skin: 'demo-class',
        closeBtn: 0
      })
      // alert('名片 工牌 合同 至少选择一种上传')
      return false
    }

    if ($("textarea[name='profiles']").val().length < 20) {
      layer.alert('自我介绍最少需要20字', {
        skin: 'demo-class',
        closeBtn: 0
      })
      $("textarea[name='profiles']").css('border-color', '#FF0000')
      return false
    }

    return true
  }

  //dialog
  $('.myprofile-example').on('click', function () {
    $('.' + $(this).data('dialog')).show()
  })
  $('.dialog').on('click', function () {
    $(this).hide()
  })
})(window, jQuery)
